package com.vonchange.water.pvm.behavior;

import com.vonchange.water.pvm.RunUtil;
import com.vonchange.water.pvm.enums.EnumLineSys;
import com.vonchange.water.pvm.execution.ActivityExecution;
import com.vonchange.water.pvm.model.definition.PvmActivity;
import com.vonchange.water.pvm.model.definition.PvmLine;

import java.util.Map;
import java.util.UUID;

public abstract class AbstractActivityBehavior implements ActivityBehavior{

    public abstract void  executeMain(ActivityExecution execution) throws Exception;
    @Override
    public void execute(ActivityExecution execution) throws Exception {
        String executionId= UUID.randomUUID().toString();
        execution.setExecutionId(executionId);
        executeMain(execution);
        if(execution.getCurrentActivity() instanceof PvmActivity&&execution.findPvmActivityAuto()){
            goLine(execution,EnumLineSys.main.name());
        }
    }

    private void goLine(ActivityExecution execution,String name) throws Exception {
        PvmActivity currentActivity = (PvmActivity) execution.getCurrentActivity();
        Map<String,PvmLine> outLines= currentActivity.getOutLines();
        if(!outLines.containsKey(name)){
            return;
        }
        PvmLine pvmLine = outLines.get(name);
        RunUtil.goLine(execution.getProcessInstance(),pvmLine, name);
    }
}
